<template>
  <div>
    <search-pane
      :show="showSearchPane"
      @search="handleSearch"
      @reset="resetQueryParams"
    />

    <table-tool-bar
      v-model="showSearchPane"
      @handleCreate="handleCreateDeviceFaultRecord"
      @refresh="fetchDeviceFaultRecordPage"
    />

    <table-card>
      <el-table
        ref="filterTable"
        :data="deviceFaultRecordList"
        style="width: 100%"
      >
        <el-table-column
          label="操作"
          fixed="right"
          width="200"
        >
          <template slot-scope="scope">
            <table-option-group
              type="button"
              @handleDelete="handleDeleteDeviceFaultRecord(scope.row.id)"
              @handleUpdate="handleUpdateDeviceFaultRecord(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="queryParams.pageNum"
        :page-sizes="[1, 2, 5, 100]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </table-card>

    <form-dialog
      v-model="dialogFormVisible"
      :type="formType"
      @create="createDeviceFaultRecord"
      @update="updateDeviceFaultRecord"
    >
      <el-form
        ref="deviceFaultRecordForm"
        :rules="formRules"
        :model="tempDeviceFaultRecord"
      />
    </form-dialog>
  </div>
</template>

<script>
import SearchPane from '@/components/SearchPane/SearchPane'
import SearchItem from '@/components/SearchPane/SearchItem'
import PDInput from '@/components/SearchPane/PDInput'
import TableCard from '@/components/table/TableCard'
import TableToolBar from '@/components/table/TableToolBar'
import { addDeviceFaultRecord, deleteDeviceFaultRecord, pageDeviceFaultRecords, updateDeviceFaultRecord } from '@/api/deviceFaultRecord'
import FormDialog from '@/components/FormDialog/FormDialog'
import TableOptionGroup from '@/components/table/TableOptionGroup'
import { showSuccessTip } from '@/utils/simple-tips'

export default {
  name: 'DeviceFaultRecord',
  components: { TableOptionGroup, FormDialog, TableToolBar, TableCard, PDInput, SearchItem, SearchPane },
  data() {
    return {
      // search attr
      showSearchPane: true,
      queryParams: {
        pageSize: 5,
        pageNum: 1
      },
      total: 10,

      // form attr
      tempDeviceFaultRecord: {

      },
      dialogFormVisible: false,
      formType: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      formRules: {

      },

      // table attr
      deviceFaultRecordList: []
    }
  },
  methods: {

    fetchDeviceFaultRecordPage() {
      pageDeviceFaultRecords(this.queryParams).then(response => {
        this.deviceFaultRecordList = response.data.list
        this.total = response.data.total
      })
    },

    // region search relative function
    handleSearch() {

    },
    resetQueryParams() {
      this.queryParams = {}
    },
    // endregion

    // region form relative function
    resetTempDeviceFaultRecord() {
      this.tempDeviceFaultRecord = {}
    },
    handleCreateDeviceFaultRecord() {
      this.resetTempDeviceFaultRecord()
      this.formType = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['deviceFaultRecordForm'].clearValidate()
      })
    },
    createDeviceFaultRecord() {
      this.$refs['deviceFaultRecordForm'].validate((valid) => {
        if (valid) {
          addDeviceFaultRecord(this.tempDeviceFaultRecord).then(() => {
            this.fetchDeviceFaultRecordPage()
            this.dialogFormVisible = false
            showSuccessTip()
          })
        }
      })
    },
    handleUpdateDeviceFaultRecord(row) {
      this.tempDeviceFaultRecord = DeviceFaultRecordect.assign({}, row)
      this.formType = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['deviceFaultRecordForm'].clearValidate()
      })
    },
    updateDeviceFaultRecord() {
      this.$refs['deviceFaultRecordForm'].validate((valid) => {
        if (valid) {
          const tempDeviceFaultRecord = Object.assign({}, this.tempDeviceFaultRecord)
          updateDeviceFaultRecord(tempDeviceFaultRecord).then(() => {
            this.fetchDeviceFaultRecordPage()
            this.dialogFormVisible = false
            showSuccessTip()
          })
        }
      })
    },
    handleDeleteDeviceFaultRecord(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDeviceFaultRecord(id).then(() => {
          this.dialogFormVisible = false
          this.fetchDeviceFaultRecordPage()
          showSuccessTip()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // endregion

    // region page relative function
    handleSizeChange(newSize) {
      this.pageSize = newSize
      this.fetchDeviceFaultRecordPage()
    },
    handleCurrentChange(newSize) {
      this.pageNum = newSize
      this.fetchDeviceFaultRecordPage()
    }
    // endregion
  }
}

</script>

<style lang="scss" scoped>
</style>
